var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var config = require('../../config');
var util = require('../../utils/util.js')
//获取应用实例
var app = getApp();
Page({
  data: {
    endRoom: config.service.endRoom,
    leaveRoom: config.service.leaveRoom,
    getRoomInfo: config.service.getRoomInfo,
    inputValue: '0',
    name:'',
    room_id:'',
    listData:[],
    room_name: '',
    isShow: true,
    remainChip: 0,
    loading: false,
    winChip: 0,
    chip: 0,
    userInfo: {}
  },
  bindKeyInput: function(e){
    this.setData({
      remainChip: e.detail.value
    });
  },
  leave: function(){
    var self = this;
    self.setData({
      isShow: false,
      isLoading: true
    });
    qcloud.request({
      url: self.data.leaveRoom,
      data: {
         room_id: self.data.room_id,
         remain_chip: self.data.remainChip
      },
      login: true,
      success(resp) {
          self.setData({
            isLoading: false
          });
          if(resp.data.code === 0){
              self.setData({
                winChip: resp.data.data.win_chip,
                chip: parseInt(self.data.remainChip) - parseInt(resp.data.data.win_chip)
              });
          }else{
              showModel('fail', resp.data.message);
          }
      },
      fail(error) {
          console.log('request fail', error);
      }
    });
  },
  endRoom: function(){
    var self = this;
    self.setData({
      isLoading: true
    });
    qcloud.request({
      url: self.data.endRoom,
      data: {
         room_id: self.data.room_id
      },
      login: true,
      success(resp) {
        self.setData({
          isLoading: false
        });
          if(resp.data.code === 0){
              qcloud.request({
                url: self.data.getRoomInfo,
                data: {
                   room_id: self.data.room_id
                },
                login: true,
                success(resp) {
                    if(resp.data.code === 0){
                      wx.navigateTo({
                        url: '../total/total?room_id='+ self.data.room_id +'&room_name='+ self.data.room_name
                      })
                    }else{
                      util.showModel('fail', resp.data.message);
                    }
                },
                fail(error) {
                    console.log('request fail');
                }
              });
          }else{
              util.showModel('fail', resp.data.message);
          }
      },
      fail(error) {
          console.log('request fail', error);
      }
    });
  },
  newRoom: function(){
    var self = this;
    wx.navigateTo({
      url: '../index/index'
    });
  },
  onLoad: function (options) {
    console.log(options);
    var self = this;
    app.getUserInfo(function (userInfo) {
      self.setData({
        userInfo: userInfo
      })
    })
    self.setData({
      room_id: options.room_id,
      room_name: options.room_name
    });
    wx.setNavigationBarTitle({
      title: self.data.userInfo.nickName +' 的战绩'
    });
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      self.setData({
        items: self.data.items,
        userInfo: userInfo
      })
    })
  }
})
